package com.edu.exam.entity.examination.control;

import com.edu.exam.enums.examination.SecurityEventEscalationStatus;
import com.edu.exam.enums.examination.SecurityEventImpactLevel;
import com.edu.exam.enums.examination.SecurityEventSeverityLevel;
import com.edu.exam.enums.examination.SecurityEventStatus;
import com.edu.exam.enums.examination.SecurityEventType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 安全事件实体类
 * 所属模块：E-01 考试执行模块
 * 对应数据库表：security_event
 *
 * <p>安全事件实体用于记录考试过程中检测到的安全事件，包括
 * 事件类型、严重级别、风险评分、处理状态等。提供完整的安全事件管理功能。</p>
 *
 * <p>核心功能：
 * - 事件记录管理：完整记录安全事件的详细信息
 * - 风险评估：计算和存储事件的风险评分
 * - 状态跟踪：跟踪事件的处理状态和进度
 * - 影响评估：评估事件的影响范围和严重程度
 * - 处理流程：管理事件的处理和确认流程</p>
 *
 * @author System
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Entity
public class SecurityEvent {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 事件ID
     *
     * <p>安全事件的唯一标识符，采用UUID格式生成。</p>
     */
    @Column(name = "event_id", nullable = false, length = 32, unique = true)
    private String eventId;

    /**
     * 任务ID
     *
     * <p>关联的监控任务ID。</p>
     */
    @Column(name = "task_id", nullable = false, length = 32)
    private String taskId;

    /**
     * 考试会话ID
     *
     * <p>关联的考试会话ID。</p>
     */
    @Column(name = "session_id", nullable = false, length = 32)
    private String sessionId;

    /**
     * 事件类型
     *
     * <p>安全事件的类型。</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "event_type", nullable = false, length = 50)
    private SecurityEventType eventType;

    /**
     * 严重级别
     *
     * <p>事件的严重程度级别。</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "severity_level", nullable = false, length = 20)
    private SecurityEventSeverityLevel severityLevel;

    /**
     * 风险评分
     *
     * <p>事件的风险评分，范围0-10。</p>
     */
    @Column(name = "risk_score", precision = 5, scale = 2)
    private Float riskScore = 0.0f;

    /**
     * 事件描述
     *
     * <p>事件的详细描述信息。</p>
     */
    @Column(name = "event_description", columnDefinition = "TEXT", length = 500)
    private String eventDescription;

    /**
     * 事件时间
     *
     * <p>事件发生的时间。</p>
     */
    @Column(name = "event_time", nullable = false)
    private LocalDateTime eventTime;

    /**
     * 检测时间
     *
     * <p>系统检测到事件的时间。</p>
     */
    @Column(name = "detection_time", nullable = false)
    private LocalDateTime detectionTime;

    /**
     * 事件状态
     *
     * <p>事件的处理状态。</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "event_status", nullable = false, length = 20)
    private SecurityEventStatus eventStatus = SecurityEventStatus.PENDING;

    /**
     * 处理者ID
     *
     * <p>处理事件的操作者ID。</p>
     */
    @Column(name = "processor_id", length = 32)
    private String processorId;

    /**
     * 处理时间
     *
     * <p>事件被处理的时间。</p>
     */
    @Column(name = "processing_time")
    private LocalDateTime processingTime;

    /**
     * 影响级别
     *
     * <p>事件的影响级别。</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "impact_level", length = 20)
    private SecurityEventImpactLevel impactLevel = SecurityEventImpactLevel.LOW;

    /**
     * 影响范围
     *
     * <p>事件的影响范围描述。</p>
     */
    @Column(name = "impact_scope", length = 500)
    private String impactScope;

    /**
     * 事件来源
     *
     * <p>事件的来源系统或模块。</p>
     */
    @Column(name = "event_source", length = 100)
    private String eventSource;

    /**
     * 检测规则ID
     *
     * <p>触发事件检测的规则ID。</p>
     */
    @Column(name = "detection_rule_id", length = 32)
    private String detectionRuleId;

    /**
     * 检测算法
     *
     * <p>用于检测事件的算法名称。</p>
     */
    @Column(name = "detection_algorithm", length = 50)
    private String detectionAlgorithm;

    /**
     * 置信度
     *
     * <p>检测结果的置信度，范围0-1。</p>
     */
    @Column(name = "confidence_level", precision = 3, scale = 2)
    private Float confidenceLevel = 0.0f;

    /**
     * 处理结果
     *
     * <p>事件处理的结果说明。</p>
     */
    @Column(name = "processing_result", length = 1000)
    private String processingResult;

    /**
     * 升级状态
     *
     * <p>事件的升级状态。</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "escalation_status", length = 20)
    private SecurityEventEscalationStatus escalationStatus = SecurityEventEscalationStatus.NOT_ESCALATED;

    /**
     * 升级时间
     *
     * <p>事件升级的时间。</p>
     */
    @Column(name = "escalation_time")
    private LocalDateTime escalationTime;

    /**
     * 事件标签
     *
     * <p>JSON格式的事件标签。</p>
     */
    @Column(name = "event_tags", columnDefinition = "TEXT")
    private String eventTags;

    /**
     * 事件备注
     *
     * <p>事件的备注信息。</p>
     */
    @Column(name = "event_notes", columnDefinition = "TEXT")
    private String eventNotes;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 创建人ID
     */
    @Column(name = "created_by", length = 50)
    private String createdBy;

    /**
     * 更新人ID
     */
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 版本号（乐观锁）
     */
    @Version
    @Column(name = "version")
    private Integer version = 0;

    /**
     * 逻辑删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;
}